package com.hdl.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.springframework.jdbc.core.JdbcTemplate;

import com.hdl.dao.NumDao;
import com.hdl.entity.Num;
import com.hdl.util.Druidutils;
import com.hdl.util.JdbcUtil;

public class NumDaoImpl implements NumDao{

	private JdbcTemplate template = new JdbcTemplate(Druidutils.getDataSource());
	
	private Connection conn = JdbcUtil.getConnection();
	private PreparedStatement pstm = null;
	private ResultSet rs = null;
	
	@Override
	public int getCount() {
		try {
			//定义sql语句
			String sql = "select * from NUM where NUM_ID = 1";
			//获取预编译对象
			pstm = conn.prepareStatement(sql);
			//执行
			rs = pstm.executeQuery();
			if(rs.next()) {
				Num num = new Num();
				num.setNumCount(rs.getInt("NUM_COUNT"));
				num.setNumId(rs.getInt("NUM_ID"));
				return num.getNumCount();
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public void saveCount(int count) {
		
		//定义sql语句
		String sql = "update NUM set NUM_COUNT = ? WHERE NUM_ID = 1 ";
		template.update(sql,count);
	}

}
